/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import 'dart:async';
import 'package:flutter/material.dart';
import '../l10n/generated/app_localizations.dart';

class FullScreenOverlayTimer extends StatefulWidget {
  const FullScreenOverlayTimer({super.key});

  @override
  State<FullScreenOverlayTimer> createState() => _FullScreenOverlayTimerState();
}

class _FullScreenOverlayTimerState extends State<FullScreenOverlayTimer>
    with SingleTickerProviderStateMixin {
  int _countdown = 3;
  late Timer _timer;
  bool _isHidLoading = false;

  @override
  void initState() {
    super.initState();
    _startTimer();
  }

  // 开启倒计时
  void _startTimer() {
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        if (_countdown > 1) {
          _countdown--;
        } else {
          _timer.cancel();
          Navigator.of(context).pop();
          setState(() {
            _isHidLoading = true;
          });
        }
      });
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 获取本地化字符串
    AppLocalizations il8n = AppLocalizations.of(context)!;
    return Offstage(
      offstage: _isHidLoading,
      child: Stack(
        children: [
          Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            color: Colors.transparent,
          ),
          Image.asset(
            'lib/assets/images/full_screen_timer.png',
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            fit: BoxFit.cover,
          ),
          Positioned(
              top: MediaQuery.of(context).padding.top + 16,
              right: 24,
              child: GestureDetector(
                onTap: () {
                  _timer.cancel();
                  Navigator.of(context).pop();
                  setState(() {
                    _isHidLoading = true;
                  });
                },
                child: Container(
                  alignment: Alignment.center,
                  width: 76,
                  height: 36,
                  decoration: BoxDecoration(
                      color: const Color.fromRGBO(0, 0, 0, 0.05),
                      borderRadius: BorderRadius.circular(18)),
                  child: Text(
                    '${_countdown}S ${il8n.skip}',
                    style: const TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                        decoration: TextDecoration.none,
                        color: Colors.white),
                  ),
                ),
              )),
        ],
      ),
    );
  }
}
